package com.baichang.taoshunda.dao.impl;

import com.baichang.framework.dao.impl.BaseDaoImpl;
import com.baichang.taoshunda.dao.ReminderDao;
import com.baichang.taoshunda.entity.Reminder;
import org.springframework.stereotype.Repository;

/**
 * Created by ${wushenglu} on 17/08/25.
 */
@Repository
public class ReminderDaoImpl extends BaseDaoImpl<Reminder> implements ReminderDao {
    /**
     * 查看用户催单次数
     *
     * @param userId  用户
     * @param orderId 订单单号
     * @return
     */
    @Override
    public int getCount(Integer userId, Integer orderId) {
        String sql = "SELECT COUNT(id) FROM t_reminder WHERE user_id=? AND order_id=?";
        return queryForInt(sql, userId, orderId);
    }

    /**
     * 查询最新的催单信息
     *
     * @param userId
     * @param orderId
     * @return
     */
    @Override
    public Reminder getEntityByUserId(Integer userId, Integer orderId) {
        String sql = "SELECT * FROM t_reminder WHERE user_id=? AND order_id=? ORDER BY id DESC LIMIT 1 ";
        return queryForBean(sql, userId, orderId);
    }

    /**
     * 根据订单编号获取催单信息
     *
     * @param orderId 订单编号
     * @return
     */
    @Override
    public Reminder getEntityByOrderId(Integer orderId) {
        String sql = " SELECT * FROM t_reminder WHERE order_id=? ORDER BY id DESC LIMIT 1 ";
        return queryForBean(sql, orderId);
    }

    @Override
    public Reminder getEntityById(Integer id) {
        String sql = " SELECT * FROM t_reminder WHERE id=? ORDER BY id DESC LIMIT 1 ";
        return queryForBean(sql, id);
    }

    /**
     * 修改催单被查看状态
     *
     * @param reminderId 催单编号
     * @return
     */
    @Override
    public int updateRemindLook(Integer reminderId) {
        String sql = " UPDATE {table} SET buss_is_look=1 WHERE id=? ";
        return update(sql, reminderId);
    }

    @Override
    public boolean updateReminderState(String orderNumber) {
        String sql = "UPDATE `t_reminder` SET `state`='1' WHERE order_id=?";
        return update(sql, orderNumber) > 0;
    }

    @Override
    public int checkIsReminderStatus(Integer id) {
        String sql = "SELECT IFNULL(state, 0) FROM t_reminder r WHERE r.order_id =? ORDER BY r.id DESC LIMIT 1";
        return queryForInt(sql, id);
    }
}
